package com.newbee.activity.tab;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.activity.LoginActivity;
import com.newbee.activity.MultChatResultListActivity;
import com.newbee.activity.UserConfigActivity;
import com.newbee.activity.VoiceFileListActivity;
import com.newbee.app.SpeechApp;
import com.newbee.round.CustomImageView;

public class NewAboutMeActivity extends Activity implements OnClickListener
{

	private CustomImageView mHead;

	private TextView mNickName;
	private TextView mEmail;

	private Bitmap headBitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aboutme);
		initUI();
		initClickListener();

	}

	private void initUI()
	{
		this.mHead = (CustomImageView) this.findViewById(R.id.aboutme_head);
		mNickName = (TextView) this.findViewById(R.id.about_nickname);
		this.mEmail = (TextView) this.findViewById(R.id.about_email);
		initUserInfo();
	}

	private void initUserInfo()
	{

		SharedPreferences sp = this.getSharedPreferences(
				LoginActivity.SP_USER_INFO, Activity.MODE_MULTI_PROCESS);
		String email = sp.getString(LoginActivity.SP_KEY_EMAIL, "error");
		String head = sp.getString(LoginActivity.SP_KEY_HEAD, null);
		String nickname = sp.getString(LoginActivity.SP_KEY_NICKNAME, "error");
		String userName = sp.getString(LoginActivity.SP_KEY_USERNAME, "error");

		mEmail.setText(email);
		mNickName.setText(userName);
		if (head != null)
		{

			File file = new File(Environment.getExternalStorageDirectory()
					+ SpeechApp.HEAD_FILE + "/" + head + ".png");

			if (file.exists())
			{
				try
				{
					FileInputStream fis = new FileInputStream(file);
					headBitmap = BitmapFactory.decodeStream(fis);
					mHead.setmSrc(headBitmap);
					mHead.postInvalidate();
					fis.close();

				}
				catch (Exception e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}

	}

	private void initClickListener()
	{

		this.findViewById(R.id.about_btn_config).setOnClickListener(this);
		this.findViewById(R.id.about_btn_contactus).setOnClickListener(this);
		this.findViewById(R.id.about_btn_luyinfile).setOnClickListener(this);
		this.findViewById(R.id.about_btn_multchatfile).setOnClickListener(this);
		this.findViewById(R.id.mult_requ_list_back).setOnClickListener(this);

	}

	@Override
	public void onClick(View v)
	{

		switch (v.getId())
		{
		case R.id.about_btn_config:
		{
			Intent intent = new Intent(this, UserConfigActivity.class);
			this.startActivity(intent);
			break;
		}
		case R.id.about_btn_contactus:
		{

			Intent intent = new Intent(Intent.ACTION_SENDTO);
			intent.setData(Uri.parse("mailto:771762167@qq.com"));
			intent.putExtra(Intent.EXTRA_SUBJECT, "语音小助手-问题反馈");
			intent.putExtra(Intent.EXTRA_TEXT, "有什么想要对我们说的，请写下来吧!(づ￣ 3￣)づ\r\n");
			startActivity(intent);

			break;
		}
		case R.id.about_btn_luyinfile:
		{
			Intent intent = new Intent(this, VoiceFileListActivity.class);
			this.startActivity(intent);
			break;
		}
		case R.id.about_btn_multchatfile:
		{
			Intent intent = new Intent(this, MultChatResultListActivity.class);
			this.startActivity(intent);
			break;
		}
		case R.id.mult_requ_list_back:
		{
			this.finish();

		}
		default:
			break;
		}

	}

	@Override
	protected void onDestroy()
	{
		this.headBitmap.recycle();
		super.onDestroy();
	}
}
